package top.infopub.mgr.sys.web;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.sys.service.MenuService;
import top.infopub.mgr.sys.service.PurviewService;
import top.infopub.model.sys.Menu;
import top.infopub.model.sys.Purview;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.StringHelper;


/**
 * 权限管理
 * @author Awoke
 * @version 2018年10月15日
 * @see PurviewController
 * @since
 */
@Controller
@RequestMapping("/purview")
public class PurviewController extends GenericController {

    @Autowired
    private PurviewService purviewService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private MenuService menuService;

    /**
     * 根据菜单ID获取权限码
     * @param menuId
     * @return
     */
    @RequestMapping("/getPurviewByMenuId")
    public @ResponseBody RspVo<Purview> getPurviewByMenuId(String menuId) {
        Purview paramPurview = new Purview();
        paramPurview.setSparentId(menuId);
        List<Purview> purviews = null;
        if (StringUtils.isNotBlank(menuId)) {
            purviews = purviewService.selectByEntityWhere(paramPurview);
        }
        return RspVo.getSuccessResponse(0, purviews);
    }

    /**
     * 根据权限编码ID获取权限
     * @param id
     * @return
     */
    @RequestMapping("/getPurviewById")
    public @ResponseBody RspVo<Purview> getPurviewById(String id) {
        Purview purview = purviewService.selectByPrimaryKey(id);
        if (purview == null) {
            purview = new Purview();
        }
        return RspVo.getSuccessResponse(purview);
    }

    /**
     * 删除权限码
     * @param checkboxId
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public @ResponseBody RspVo<String> delete(String[] checkboxId) {
        purviewService.delete(checkboxId);
        return RspVo.getSuccessResponse();
    }

    /**
     * 保存/修改权限码
     * @param purview 权限码对象
     * @return
     */
    @RequestMapping("/save")
    @SuppressWarnings("unchecked")
    public @ResponseBody RspVo<Purview> save(Purview purview) {
        if (StringUtils.isBlank(purview.getSparentId())) {
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("菜单ID为空~");
        }
        Menu menu = menuService.selectByPrimaryKey(purview.getSparentId());
        if (null == menu) {
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("父菜单不存在~");
        }
        purview.setSparentName(menu.getSname());
        // 执行保存
        if (StringUtils.isBlank(purview.getId())) {
            if (purviewService.isNameExist(purview)) {
                return ErrorCodeEnum.ERROR_COMMON_UNIQUE.getRspVo("权限编码已经存在~");
            }
            purview.setSpurNo(codeGeneratorConfigService.generatorCode("sys_purview"));
            purview.setDaddDate(new Date());
            purview.setSpyName(StringHelper.getFullSpell(purview.getSpurName()));
            purview.setSjpName(StringHelper.getFirstSpell(purview.getSpurName()));
            purviewService.insert(purview);
        }
        else {// 执行更新
            Purview presitPurview = purviewService.selectByPrimaryKey(purview.getId());
            if (presitPurview != null) {
                if (!presitPurview.getSpurCode().equalsIgnoreCase(purview.getSpurCode())) {
                    if (purviewService.isNameExist(purview)) {
                        return ErrorCodeEnum.ERROR_COMMON_UNIQUE.getRspVo("权限编码已经存在~");
                    }
                }
            }
            purview.setSpyName(StringHelper.getFullSpell(purview.getSpurName()));
            purview.setSjpName(StringHelper.getFirstSpell(purview.getSpurName()));
            purviewService.updateBySelective(purview);
        }
        return RspVo.getSuccessResponse(purview);
    }

}
